package com.itboye.jinpuyiyuan.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.OrderDetailBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/9/17 0017.
 */

public class OrderDetailActivity extends BaseActivity implements Observer {

    //title
    private ImageView back;
    private TextView tvTitle;

    private TextView orderNumTv,//订单编号
            orderTimeTv,  //订单时间
            orderDetailName, //订单详情
            payManeyTv,//支付金额
            payTypeTv,//支付类型
            payStatusTv;//支付状态
    private BranchAddressPresenter presenter;
    private String uid;
    private String order_id;
    private OrderDetailBean data;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_orderdetail);
        tvTitle.setText("订单详情");

        order_id=getIntent().getStringExtra("order_id");
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);

        presenter.getOrderDetail(uid,order_id);

    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity!=null){
            try{
                closeProgressDialog();
            }catch (Exception e){

            }
            if (entity.getEventType() == BranchAddressPresenter.By_Order_orderDetail_success){ //订单详情
//                orderNumTv,//订单编号
//                        orderTimeTv,  //订单时间
//                        orderDetailName, //订单详情
//                        payManeyTv,//支付金额
//                        payTypeTv;//支付类型
                data = (OrderDetailBean) entity.getData();
                orderNumTv.setText(data.getOrder_code());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

                orderTimeTv.setText(format.format(new Date(Long.parseLong(data.getCreate_time())*1000)));
                orderDetailName.setText(data.getDetail());
                if (data.getPay_status().equals("1")){//已付款
                    payStatusTv.setVisibility(View.VISIBLE);
                    payStatusTv.setText("申请退款");
                }else {//未付款
                    payStatusTv.setVisibility(View.GONE);
                }
                DecimalFormat df = new DecimalFormat("#0.00");
                payManeyTv.setText(df.format(Double.parseDouble(data.getPay_money())/100));
                if (data.getPay_type().trim().equals("1")){//微信
                    payTypeTv.setText("微信支付");
                }else if (data.getPay_type().trim().equals("2")){//支付宝
                    payTypeTv.setText("支付宝支付");
                }

            }else if (entity.getEventType()== BranchAddressPresenter.By_Order_orderDetail_fail){

                MAlert.alert(entity.getMsg());
            }else if (entity.getEventType()== BranchAddressPresenter.By_Order_OrderRefund_success){//申请退款
                MAlert.alert(entity.getData());
                payStatusTv.setEnabled(true);
                presenter.getOrderDetail(uid,order_id);
            }else if (entity.getEventType()== BranchAddressPresenter.By_Order_OrderRefund_fail){
                MAlert.alert(entity.getMsg());
                payStatusTv.setEnabled(true);
            }
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.back:
                this.finish();
                break;

            case R.id.payStatusTv: //申请退款
                payStatusTv.setEnabled(false);
                showProgressDialog("退款申请中...",false);
                presenter.rebackMoney(uid,data.getOrder_code());
                break;
        }
    }
}
